package com.syh.jn.scm.service.impl.upstream.base;

import cn.redscarf.commons.util.bean.ExtBeanUtils;
import com.alibaba.dubbo.config.annotation.Service;
import com.syh.core.ddd.infrastructure.repository.AggregateRepository;
import com.syh.core.ddd.service.AbstractServiceImpl;
import com.syh.jn.scm.domain.upstream.entity.base.logistics.UpLogistics;
import com.syh.jn.scm.domain.upstream.entity.base.logistics.UpLogisticsAddress;
import com.syh.jn.scm.domain.upstream.entity.base.logistics.UpLogisticsTruck;
import com.syh.jn.scm.domain.upstream.repository.base.UpLogisticsRepository;
import com.syh.jn.scm.dto.upstream.base.logistics.UpLogisticsMto;
import com.syh.jn.scm.dto.upstream.base.logistics.UpLogisticsSto;
import com.syh.jn.scm.dto.upstream.base.logistics.UpLogisticsVo;
import com.syh.jn.scm.dto.upstream.base.logistics.logisticsAddress.UpLogisticsAddressMto;
import com.syh.jn.scm.dto.upstream.base.logistics.logisticsAddress.UpLogisticsAddressSto;
import com.syh.jn.scm.dto.upstream.base.logistics.logisticsAddress.UpLogisticsAddressVo;
import com.syh.jn.scm.dto.upstream.base.logistics.logisticsTruck.UpLogisticsTruckMto;
import com.syh.jn.scm.dto.upstream.base.logistics.logisticsTruck.UpLogisticsTruckSto;
import com.syh.jn.scm.dto.upstream.base.logistics.logisticsTruck.UpLogisticsTruckVo;
import com.syh.jn.scm.service.api.upstream.base.UpLogisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 物流接口实现
 */
@Service
@Transactional
public class UpLogisticsServiceImpl extends AbstractServiceImpl<UpLogisticsSto, UpLogisticsMto, UpLogisticsVo, UpLogistics, Long> implements UpLogisticsService {

  @Autowired
  private UpLogisticsRepository upLogisticsRepositoryImpl;

  @Override
  protected AggregateRepository<UpLogistics, Long> getAggregateRepository() {
    return upLogisticsRepositoryImpl;
  }

  /**
   * 增加物流信息
   * @param sto
   * @return
   */
  @Override
  public Long save(UpLogisticsSto sto) {
    UpLogistics logistics = new UpLogistics(sto);
    Long id = upLogisticsRepositoryImpl.add(logistics);

    UpLogistics logisticsMq = upLogisticsRepositoryImpl.getById(id);
    Object[] obj = new Object[]{logisticsMq};
//    //发送事件
//    this.publicEvent(LogisticsEvent.buildLogisticsEvent(obj));

    return id;
  }

  /**
   * 修改物流信息
   * @param mto
   */
  @Override
  public void update(UpLogisticsMto mto) {
    UpLogistics logistics = upLogisticsRepositoryImpl.getById(mto.getId());
    logistics.modify(mto);
    upLogisticsRepositoryImpl.update(logistics);
  }

  /**
   * 删除物流信息
   * @param id
   */
  @Override
  public void delete(Long id) {
    UpLogistics logistics = upLogisticsRepositoryImpl.getById(id);
    logistics.destroy();
    upLogisticsRepositoryImpl.remove(logistics);
  }

  /**
   * 启用
   * @param id
   */
  @Override
  public void enabled(Long id) {
    UpLogistics logistics = upLogisticsRepositoryImpl.getById(id);
    logistics.enabled();
    upLogisticsRepositoryImpl.update(logistics);
  }

  /**
   * 停用
   * @param id
   */
  @Override
  public void disabled(Long id) {
    UpLogistics logistics = upLogisticsRepositoryImpl.getById(id);
    logistics.disabled();
    upLogisticsRepositoryImpl.update(logistics);
  }

  /**
   * 批量增加地址
   * @param stoList
   */
  @Override
  public void batchSaveAddress(List<UpLogisticsAddressSto> stoList) {
    List<UpLogisticsAddress> addressesList = new ArrayList<>();
    stoList.stream().forEach((sto)->{
      UpLogisticsAddress address = new UpLogisticsAddress(sto);
      addressesList.add(address);
    });
    upLogisticsRepositoryImpl.batchSaveAddress(addressesList);
  }

  /**
   * 批量修改地址
   * @param mtoList
   */
  @Override
  public void batchModifyAddress(List<UpLogisticsAddressMto> mtoList) {
    List<UpLogisticsAddress> addressesList = new ArrayList<>();
    mtoList.stream().forEach((mto)->{
      UpLogisticsAddress address = upLogisticsRepositoryImpl.getByAddressId(mto.getId());
      address.modify(mto);
      addressesList.add(address);
    });
    upLogisticsRepositoryImpl.batchModifyAddress(addressesList);
  }

  /**
   * 根据地址id删除
   * @param addressId
   */
  @Override
  public void deleteAddress(Long addressId) {
    UpLogisticsAddress address = upLogisticsRepositoryImpl.getByAddressId(addressId);
    address.destroy();
    upLogisticsRepositoryImpl.deleteAddress(address);
  }

  /**
   * 批量添加服务车型
   * @param stoList
   */
  @Override
  public void batchSaveTruck(List<UpLogisticsTruckSto> stoList) {
    List<UpLogisticsTruck> truckList = new ArrayList<>();
    stoList.stream().forEach((sto)->{
      UpLogisticsTruck truck = new UpLogisticsTruck(sto);
      truckList.add(truck);
    });
    upLogisticsRepositoryImpl.batchSaveTruck(truckList);
  }

  /**
   * 批量修改服务车型
   * @param mtoList
   */
  @Override
  public void batchModifyTruck(List<UpLogisticsTruckMto> mtoList) {
    List<UpLogisticsTruck> truckList = new ArrayList<>();
    mtoList.stream().forEach((mto)->{
      UpLogisticsTruck truck = upLogisticsRepositoryImpl.getByTruckId(mto.getId());
      truck.modify(mto);
      truckList.add(truck);
    });
    upLogisticsRepositoryImpl.batchModifyTruck(truckList);
  }

  /**
   * 根据车型id删除
   * @param truckId
   */
  @Override
  public void deleteTruck(Long truckId) {
    UpLogisticsTruck truck = upLogisticsRepositoryImpl.getByTruckId(truckId);
    truck.destroy();
    upLogisticsRepositoryImpl.deleteTruck(truck);
  }

  /**
   * 根据物流id查地址列表
   * @param logisticsId
   * @return
   */
  @Override
  public List<UpLogisticsAddressVo> getAddressListById(Long logisticsId) {
    return upLogisticsRepositoryImpl.getAddressListById(logisticsId);
  }

  /**
   * 根据物流id查车型列表
   * @param logisticsId
   * @return
   */
  @Override
  public List<UpLogisticsTruckVo> getTruckListById(Long logisticsId) {
    return upLogisticsRepositoryImpl.getTruckListById(logisticsId);
  }

  /**
   * 根据车型id获取车型
   * @param truckId
   * @return
   */
  @Override
  public UpLogisticsTruckVo getByTruckId(Long truckId) {
    UpLogisticsTruck entity = upLogisticsRepositoryImpl.getByTruckId(truckId);
    UpLogisticsTruckVo vo = new UpLogisticsTruckVo();
    ExtBeanUtils.copyProperties(entity, vo);
    return vo;
  }

  /**
   * 根据地址id获取地址
   * @param addressId
   * @return
   */
  @Override
  public UpLogisticsAddressVo getByAddressId(Long addressId) {
    UpLogisticsAddress entity = upLogisticsRepositoryImpl.getByAddressId(addressId);
    UpLogisticsAddressVo vo = new UpLogisticsAddressVo();
    ExtBeanUtils.copyProperties(entity, vo);
    return vo;
  }
}
